import * as fs from "fs";
import * as path from "path";
import { KOA_STATIC_CACHE_PATH } from "../config/const"
import { readdir,stat,access  } from 'fs/promises';
import { LSFS } from "./LSFS"
class _FsCon {
    //检测是否有目录存在，没有则创建对应目录解构  fs.PathLike
   async createDirs(name: string,_path:string = KOA_STATIC_CACHE_PATH) {
       const PATH = path.resolve(_path,name)
       console.log("PATH:", PATH);
       
       try {
           
        //    let isDir = await stat(PATH);
        //    let isDir =  await access(PATH);
        // 
        //1、读取文件夹,判断这个文件夹是否存在
       const res = await LSFS.isDir(PATH)
       console.log("res:", res);
       return
           fs.stat(PATH, (err, state) => {
               
               if(err) {
                console.log("err:", err);
               } else {
                console.log("isDir:",state.isDirectory());
               }
           })
        //    let {err} = await stat(PATH)
        //    console.log("isDir111:",isDir);
        //    if(!isDir) return
        //    console.log("isDir:",isDir.isDirectory());
        return
           
        const files = await readdir(PATH);
        console.log("files:", files);
        
        for (let file of files){
          console.log(file);
          let f = fs.statSync(path.resolve(KOA_STATIC_CACHE_PATH,file));
         let res = f.isDirectory();
         console.log("res:", res);
        }
      } catch (err) {
        console.error(err);
      }
    }
}

const FsCon = new _FsCon();

export {
    FsCon
}